home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
PROGRAMM
/
COBOL
/
H309.ZIP
/
PSICO.ZIP
/
PSICO12.EXE
/
BINTREE.STR
next >
Wrap
Text File
|
1988-09-27
|
9KB
|
272 lines
IDENTIFICATION DIVISION.
PROGRAM-ID. BINTREE.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER. ANY-COMPUTER.
OBJECT-COMPUTER. ANY-COMPUTER.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN DISK
DELIMITER STANDARD.
*
DATA DIVISION.
FILE SECTION.
*
FD IN-FILE
LABEL RECORDS STANDARD
VALUE OF FILE-ID INFILE-NAME.
01 IN-RECORD PIC X(20).
*
WORKING-STORAGE SECTION.
01 INFILE-NAME PIC X(14).
01 BA-PSV PIC S9(4) COMP.
01 STACK-POINTER PIC S9(4) COMP.
01 ARRAYS.
03 STATE-VARIABLE OCCURS 100
PIC S9(4) COMP.
03 SAVED-POINTER OCCURS 100
PIC S9(4) COMP.
01 WS-EOF PIC 9.
88 END-OF-FILE VALUE 1.
01 ITEM-INDEX PIC S9(4) COMP.
01 CURRENT-POINTER PIC S9(4) COMP.
01 OWNER-POINTER PIC S9(4) COMP.
01 WS-TYPE PIC X.
01 WS-OPT PIC X.
01 TREE-STRUCTURE.
03 TREE-BRANCH OCCURS 100.
05 LEFT-POINTER PIC S9(4) COMP.
05 RIGHT-POINTER PIC S9(4) COMP.
05 ITEM PIC X(20).
*
*
PROCEDURE DIVISION.
PROC AA-MAIN
PROCBEGIN
*
* This procedure passes every record on the input file to a
* multi-state routine which builds them into a binary tree.
*
* At the end of input, the program will accept a code
* and will display the data in appropriate sequence
* using recursive routines to read the tree in the correct order
*
* A for ascending sequence
* D for descending sequence
* F for finish
*
SEQUENCE
DISPLAY "Enter filename, must be in current directory "
ACCEPT INFILE-NAME
OPEN INPUT IN-FILE
MOVE ZERO TO WS-EOF
*
* Note the read-ahead technique, ie read the first record
* before entering the loop. The loop logic is then
*
* until end of file
* process current record
* read next
* repeat
*
* This technique can often simplify and improve programs
*
READ IN-FILE
AT END MOVE 1 TO WS-EOF.
ITERUNTIL END-OF-FILE
BEGIN
PERFORM BA-BUILD-TREE
READ IN-FILE
AT END MOVE 1 TO WS-EOF.
REPEAT
CLOSE IN-FILE.
PERFORM BA-BUILD-TREE
MOVE "X" TO WS-OPT
ITERUNTIL WS-OPT = "F"
BEGIN
DISPLAY "Enter A (ascending),"
" D (descending) or F (finish) "
ACCEPT WS-OPT
MOVE 0 TO STACK-POINTER
MOVE 1 TO CURRENT-POINTER
SELECT
CASE WS-OPT = "A"
BEGIN
PERFORM CA-PRINT-TREE
CASE WS-OPT = "D"
BEGIN
PERFORM DA-PRINT-TREE
FI
REPEAT
STOP RUN
FI
PROCEND
IPROC BA-BUILD-TREE
PSVNAME BA-PSV
PROCBEGIN
SEQUENCE
*
* This is a multi-state routine which builds the binary tree.
* It is called each time a record is read, and inserts it into
* the correct place in the tree.
*
* Put the first item into the tree
MOVE 1 TO ITEM-INDEX
MOVE IN-RECORD TO ITEM (ITEM-INDEX)
MOVE ZERO TO LEFT-POINTER (ITEM-INDEX)
RIGHT-POINTER (ITEM-INDEX)
MOVE 1 TO CURRENT-POINTER
*
* Return to calling routine to get the next record
*
SREAD
*
ITERUNTIL END-OF-FILE
BEGIN
SELECT CASE ITEM-INDEX > 99
BEGIN
DISPLAY "Too many items in file (max 100)"
DISPLAY IN-RECORD " ignored "
OTHERWISE
ADD 1 TO ITEM-INDEX
MOVE IN-RECORD TO ITEM (ITEM-INDEX)
MOVE ZERO TO LEFT-POINTER (ITEM-INDEX)
RIGHT-POINTER (ITEM-INDEX)
MOVE 1 TO CURRENT-POINTER
ITERUNTIL CURRENT-POINTER = ZERO
BEGIN
MOVE CURRENT-POINTER TO OWNER-POINTER
SELECT
CASE IN-RECORD < ITEM (CURRENT-POINTER)
BEGIN
MOVE LEFT-POINTER (CURRENT-POINTER)
TO CURRENT-POINTER
MOVE "L" TO WS-TYPE
CASE IN-RECORD = ITEM (CURRENT-POINTER)
BEGIN
DISPLAY "EQUAL KEYS MAY CAUSE ERRORS"
MOVE LEFT-POINTER (CURRENT-POINTER)
TO CURRENT-POINTER
MOVE "L" TO WS-TYPE
OTHERWISE
MOVE RIGHT-POINTER (CURRENT-POINTER)
TO CURRENT-POINTER
MOVE "R" TO WS-TYPE
FI
REPEAT
MOVE IN-RECORD TO ITEM (ITEM-INDEX)
SELECT
CASE WS-TYPE = "L"
BEGIN
MOVE ITEM-INDEX
TO LEFT-POINTER (OWNER-POINTER)
OTHERWISE
MOVE ITEM-INDEX
TO RIGHT-POINTER (OWNER-POINTER)
FI
FI
*
* Get the next record from the calling routine
*
SREAD
REPEAT
FI
PROCEND
RPROC CA-PRINT-TREE
PSVNAME STATE-VARIABLE
PSVTHREAD STACK-POINTER
PROCBEGIN
*
* This is a recursive routine which reads the tree, displaying
* the contents in ascending sequence.
*
* The verb RCALL performs the recursive call
*
MOVE CURRENT-POINTER TO SAVED-POINTER (STACK-POINTER)
*
* Process the left branch
*
SELECT
CASE LEFT-POINTER (CURRENT-POINTER) > 0
BEGIN
MOVE LEFT-POINTER (CURRENT-POINTER)
TO CURRENT-POINTER
RCALL
FI
*
* Now display the current item
*
MOVE SAVED-POINTER (STACK-POINTER) TO CURRENT-POINTER
DISPLAY ITEM (CURRENT-POINTER)
*
* Process the right branch
*
SELECT
CASE RIGHT-POINTER (CURRENT-POINTER) > 0
BEGIN
MOVE RIGHT-POINTER (CURRENT-POINTER)
TO CURRENT-POINTER
RCALL
FI
PROCEND
RPROC DA-PRINT-TREE
PSVNAME STATE-VARIABLE
PSVTHREAD STACK-POINTER
PROCBEGIN
*
* This is a recursive routine which reads the tree, displaying
* the contents in descending sequence.
*
* The verb RCALL performs the recursive call
*
MOVE CURRENT-POINTER TO SAVED-POINTER (STACK-POINTER)
*
* Process the RIGHT branch
*
SELECT
CASE RIGHT-POINTER (CURRENT-POINTER) > 0
BEGIN
MOVE RIGHT-POINTER (CURRENT-POINTER)
TO CURRENT-POINTER
RCALL
FI
*
* Now display the current item
*
MOVE SAVED-POINTER (STACK-POINTER) TO CURRENT-POINTER
DISPLAY ITEM (CURRENT-POINTER)
*
* Process the LEFT branch
*
SELECT
CASE LEFT-POINTER (CURRENT-POINTER) > 0
BEGIN
MOVE LEFT-POINTER (CURRENT-POINTER)
TO CURRENT-POINTER
RCALL
FI
PROCEND
END PROGRAM BINTREE.